//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.quickserver.net.client.monitoring.impl;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import org.quickserver.net.client.BlockingClient;
import org.quickserver.net.client.DummyTrustManager;
import org.quickserver.net.client.Host;
import org.quickserver.net.client.HttpHost;
import org.quickserver.net.client.monitoring.HostMonitor;

public class HttpMonitor implements HostMonitor {
    private static final Logger logger = Logger.getLogger(HttpMonitor.class.getName());
    private SSLSocketFactory sslSocketFactory;
    private TrustManager[] trustManager;
    private SSLContext sslContext;
    static HostnameVerifier vf = new HostnameVerifier() {
        public boolean verify(String hostName, SSLSession session) {
            return true;
        }
    };

    public HttpMonitor() {
    }

    public char monitor(Host host) {
        HttpHost httpHost = (HttpHost)host;
        HttpURLConnection http = null;
        BufferedInputStream bis = null;
        URL url = null;

        try {
            String urlString = null;
            if (httpHost.isSecure()) {
                urlString = "https://";
            } else {
                urlString = "http://";
            }

            urlString = urlString + httpHost.getInetAddress().getHostName() + ":" + httpHost.getInetSocketAddress().getPort() + httpHost.getUri();
            url = new URL(urlString);
            http = (HttpURLConnection)url.openConnection();
            if (httpHost.isSecure()) {
                HttpsURLConnection https = (HttpsURLConnection)http;
                this.makeSSLSocketFactory();
                https.setSSLSocketFactory(this.sslSocketFactory);
                https.setHostnameVerifier(vf);
            }

            http.setRequestMethod("GET");
            http.setDoOutput(true);
            http.setReadTimeout(httpHost.getTimeout());
            http.setInstanceFollowRedirects(false);
            http.connect();
            String httpResponseCode = "" + http.getResponseCode();
            if (!httpHost.isValidHttpStatusCode(httpResponseCode)) {
                logger.log(Level.FINE, "StatusCode does not match.. got {0}, expected: {1} for host {2}", new Object[]{httpResponseCode, httpHost.getHttpStatusCode(), httpHost.getInetAddress().getHostName()});
                return 'D';
            }

            if (httpHost.getTextToExpect() != null) {
                InputStream is = http.getErrorStream();
                if (is == null) {
                    is = http.getInputStream();
                }

                bis = new BufferedInputStream(is);
                byte[] data = BlockingClient.readInputStream(bis);
                String textGot = null;
                if (data != null) {
                    textGot = new String(data, "utf-8");
                }

                if (textGot.indexOf(httpHost.getTextToExpect()) != -1) {
                    return 'A';
                }

                logger.log(Level.FINE, "{0} Error: Text [{1}] Not found! Got: {2} for host {3}", new Object[]{httpHost, httpHost.getTextToExpect(), textGot, httpHost.getInetAddress().getHostName()});
                logger.log(Level.FINEST, "Got Data: {0}", textGot);
                return 'D';
            }

            return 'A';
        } catch (IOException var24) {
            logger.log(Level.FINE, "url {0}", new Object[]{url});
            logger.log(Level.FINE, httpHost + " IOError: " + var24, var24);
            return 'D';
        } catch (Exception var25) {
            logger.log(Level.WARNING, httpHost + " Error: " + var25, var25);
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException var23) {
                    Logger.getLogger(HttpMonitor.class.getName()).log(Level.SEVERE, (String)null, var23);
                }
            }

            if (http != null) {
                http.disconnect();
            }

            host.setLastCheckedOn(new Date());
        }

        return 'E';
    }

    public void makeSSLSocketFactory() throws Exception {
        if (this.sslContext == null && this.getSslSocketFactory() == null) {
            this.sslContext = SSLContext.getInstance("SSLv3");
            if (this.trustManager == null) {
                this.trustManager = new TrustManager[]{DummyTrustManager.getInstance()};
            }

            this.sslContext.init(new KeyManager[0], this.trustManager, new SecureRandom());
        }

        if (this.getSslSocketFactory() == null) {
            this.sslSocketFactory = this.sslContext.getSocketFactory();
        }

    }

    public SSLSocketFactory getSslSocketFactory() {
        return this.sslSocketFactory;
    }

    public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
        this.sslSocketFactory = sslSocketFactory;
    }
}
